package com.example.disease.ui.web;

import androidx.fragment.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebResourceRequest;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.example.disease.R;

public class WebFragment extends Fragment {
    private WebView webView;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_web, container, false);
        // 初始化 WebView
        webView = view.findViewById(R.id.webView);
        setWebView(webView);
        // 加载网页
        webView.loadUrl("http://10.128.22.113:9008/disease");
        return view;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        if (webView != null) {
            webView.destroy();
        }
    }

    private void setWebView(WebView webView){
        // 移除 WebView 的默认边距
        webView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
        webView.setPadding(0, 0, 0, 0);

        // 设置 WebView 属性
        WebSettings webSettings = webView.getSettings();
        webSettings.setJavaScriptEnabled(true);
        webSettings.setCacheMode(WebSettings.LOAD_DEFAULT); // 设置缓存模式
        webSettings.setDomStorageEnabled(true); // 启用 DOM 存储
        webSettings.setLoadWithOverviewMode(true); // 缩放至屏幕宽度
        webSettings.setUseWideViewPort(true); // 使用宽视口

        // 设置 WebViewClient 来处理链接点击等事件
        webView.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
                if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
                    view.loadUrl(request.getUrl().toString());
                } else {
                    view.loadUrl(request.getUrl().toString());
                }
                return true;
            }

            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
                // 注入 CSS 样式，移除顶部边距
                String css = "body { margin: 0 !important; padding: 0 !important; }";
                String js = "var style = document.createElement('style'); style.innerHTML = '" + css + "'; document.head.appendChild(style);";
                view.loadUrl("javascript:" + js);
            }


            @Override
            public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
                // 处理错误，比如显示一个错误页面或提示用户
                Toast.makeText(requireContext(), "网页加载失败: " + description, Toast.LENGTH_SHORT).show();
            }
        });
    }

}
